#include "test_mapper.h"

#include "mysql_result.h"

TestMapper::TestMapper(MySQLPool *pool) : pool_(pool)
{
}

void TestMapper::createTable()
{
    auto        conn = pool_->acquire();
    std::string sql  = "CREATE TABLE IF NOT EXISTS test (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))";
    conn->Create(sql);
}

void TestMapper::dropTable()
{
    auto        conn = pool_->acquire();
    std::string sql  = "DROP TABLE IF EXISTS test";
    conn->Drop(sql);
}

int TestMapper::insertTest(const TestModel &model)
{
    auto        conn = pool_->acquire();
    std::string sql  = "INSERT INTO test (name) VALUES ('" + model.name + "')";
    return conn->Insert(sql);
}

std::vector<TestModel> TestMapper::findTestAll()
{
    std::vector<TestModel> models;

    auto        conn = pool_->acquire();
    std::string sql  = "SELECT id, name FROM test";
    MySQLResult res  = conn->Select(sql);

#if 0
    for (auto it = res.begin(); it != res.end(); ++it) {
        TestModel model;
        model.id   = std::stoull(it[0]);
        model.name = std::string(it[1]);
        models.push_back(model);
    }
#else
    for (auto &it : res) {
        TestModel model;
        model.id   = std::stoull(it[0]);
        model.name = std::string(it[1]);
        models.push_back(model);
    }
#endif
    return models;
}